import 'package:flutter/material.dart';
import 'package:flutter_widget/src/config/color_define.dart';

class RedDotWidget extends StatelessWidget {
  final int unreadCount;//未读消息数量
  final double textSize;//字体大小
  final Color textColor;//字体颜色
  final double height;//高度
  final Color backgroundColor;//背景颜色
  final bool showRedDot;//是否显示
  final int unreadMarker;//是显示小红点还是数字，1小红点 2数字

  const RedDotWidget({
    Key? key,
    int? unreadCount,
    double? textSize,
    Color? textColor,
    double? height,
    Color? backgroundColor,
    bool? showRedDot,
    int? unreadMarker,
  })  : unreadCount = unreadCount ?? 0,
        textColor = textColor ?? FWColorPicker.textWhite,
        textSize = textSize ?? 7.5,
        height = height ?? 15,
        backgroundColor = backgroundColor ?? FWColorPicker.colorFF9C4C3A,
        showRedDot = showRedDot ?? true,
        unreadMarker = unreadMarker ?? 2,
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Visibility(
      visible: showRedDot,
      child: unreadMarker == 2 ? Container(
          constraints: unreadCount < 10
              ? BoxConstraints(minWidth: height, maxWidth: height)
              : BoxConstraints(minWidth: height),
          padding: unreadCount < 10 ? null : const EdgeInsets.only(left: 3, right: 3),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(height / 2),
              color: backgroundColor),
          height: height,
          alignment: Alignment.center,
          child: Text(
            unreadCount > 99 ? '99+' : '$unreadCount',
            style: TextStyle(
              fontSize: textSize,
              color: textColor,
              height: 1.15,
            ),
            textAlign: TextAlign.center,
          )) :
      Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(height / 2),
          color: backgroundColor
        ),
        height: height,
        width: height,
      ),
    );
  }
}
